package com.alibaba.sample.kantiezi.biz.store.manager;

import com.alibaba.sample.kantiezi.dal.dao.CategoryDao;
import com.alibaba.sample.kantiezi.dal.dao.ItemDao;
import com.alibaba.sample.kantiezi.dal.dao.ProductDao;
import com.alibaba.sample.kantiezi.dal.dataobject.Category;
import com.alibaba.sample.kantiezi.dal.dataobject.Item;
import com.alibaba.sample.kantiezi.dal.dataobject.Product;

import java.util.Iterator;
import java.util.List;

public class StoreManagerImpl implements StoreManager {
    private CategoryDao categoryDao;
    private ProductDao  productDao;
    private ItemDao     itemDao;

    public void setCategoryDao(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    public void setItemDao(ItemDao itemDao) {
        this.itemDao = itemDao;
    }

    public List getAllCategories() {
        List catList = categoryDao.getCategoryList();

        for (Iterator i = catList.iterator(); i.hasNext();) {
            Category cat      = (Category) i.next();
            List     prodList = productDao.getProductListByCategoryId(cat.getCategoryId());

            cat.setProductList(prodList);
        }

        return catList;
    }

    public List getAllItems(String productId) {
        return itemDao.getItemListByProductId(productId);
    }

    public Item getItem(String itemId) {
        Item item = itemDao.getItemById(itemId);

        if (item == null) {
            return null;
        }

        Product  product  = productDao.getProductById(item.getProductId());
        Category category = categoryDao.getCategoryById(product.getCategoryId());

        product.setCategory(category);
        item.setProduct(product);

        return item;
    }
}
